package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.QueryHypertextContentDetailReq;
import com.sg.entity.HypertextContent;
import com.sg.service.base.framework.MHypertextContentService;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.HypertextContentMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MHypertextContentServiceImpl
        extends ServiceImpl<HypertextContentMapper, HypertextContent>
        implements MHypertextContentService {

    @Autowired
    HypertextContentMapper hypertextContentMapper;

    /**
     * 1-2-15查询超文本内容详情[3133]
     * gen by moon at 10/16/2022, 1:02:18 AM
     */
    @Trace(operationName = "1-2-15查询超文本内容详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HypertextContent queryHypertextContentDetail(QueryHypertextContentDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<HypertextContent> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<HypertextContent> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                HypertextContent retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-15新增底层框架超文本内容[6646]
     * gen by moon at 5/13/2023, 9:07:10 PM
     */
    @Trace(operationName = "1-2-15新增底层框架超文本内容")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFrameworkHypertextContent(HypertextContent hypertextContent) {
        hypertextContent.setHypertextContentId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        hypertextContent.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        hypertextContent.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        hypertextContent.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        hypertextContent.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        hypertextContent.setIsValid("FALSE");
        hypertextContent.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        hypertextContent.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(hypertextContent)) {
            return hypertextContent.getHypertextContentId();
        } else {
            return "-1";
        }

    }

}

